﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.SqlServer.Repositories.Readers
{
	using System.Data.SqlClient;
	using Falcon.Core.Domain;
	using Falcon.Core.SqlServer.Repositories.Readers;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Enums;

	public class ForumTopicTagWithTopicReader : IReader<ForumTopicTag>
	{
		/// <summary>
		/// Populates the specified reader.
		/// </summary>
		/// <param name="reader">The reader.</param>
		/// <param name="entity">The entity.</param>
		public void Populate(SqlDataReader reader, ForumTopicTag entity)
		{
			entity.Id = reader.GetInt32(0);
			entity.Slug = reader.GetString(1);

			entity.Topic = new ForumTopic
			{
				Id = reader.GetInt32(2),
				ForumId = reader.GetInt32(3),
				Slug = reader.GetString(4),
				Replies = reader.GetInt32(5),
				Views = reader.GetInt32(6),
				Scope = (ForumTopicScope) reader.GetInt32(7),
				Status = (ForumTopicStatus) reader.GetInt32(8),
				Created = reader.GetDateTime(9),
				Updated = reader.GetDateTime(10),
				Forum = new Forum
				{
					Id = reader.GetInt32(11),
					Title = reader.GetString(12),
					Slug = reader.GetString(13),
				}
			};

			if (!reader.IsDBNull(14))
			{
				entity.Topic.Post = new ForumPost
				{
					Id = reader.GetInt32(14),
					Title = reader.GetString(15),
					Slug = reader.GetString(16),
					Created = reader.GetDateTime(17),
					Updated = reader.GetDateTime(18),
					User = new User
					{
						Id = reader.GetInt32(19),
						Name = reader.GetString(20),
						Slug = reader.GetString(21),
						Profile = new UserProfile { Title = reader.GetString(22) }
					}
				};

				if (!reader.IsDBNull(23))
				{
					entity.Topic.Post.User.Profile.Avatar = new UserAvatar
					{
						FileName = reader.GetString(23),
						FileDiskName = reader.GetString(24),
						FilePath = reader.GetString(25),
						FileType = reader.GetString(26)
					};
				}
			}

			if (reader.IsDBNull(27))
			{
				return;
			}

			entity.Topic.LastPost = new ForumPost
			{
				Id = reader.GetInt32(27),
				Title = reader.GetString(28),
				Slug = reader.GetString(29),
				Created = reader.GetDateTime(30),
				Updated = reader.GetDateTime(31),
				User = new User
				{
					Id = reader.GetInt32(32),
					Name = reader.GetString(33),
					Slug = reader.GetString(34),
					Profile = new UserProfile { Title = reader.GetString(35) }
				}
			};

			if (!reader.IsDBNull(36))
			{
				entity.Topic.LastPost.User.Profile.Avatar = new UserAvatar
				{
					FileName = reader.GetString(36),
					FileDiskName = reader.GetString(37),
					FilePath = reader.GetString(38),
					FileType = reader.GetString(39)
				};
			}
		}
	}
}